package model.constraints.XDFC;

import java.util.ArrayList;

public class Expressions {
	
	public boolean checkLink(String operator){
		if (operator.equals("and"))
			return true;
		if (operator.equals("or")) 
			return true;
		
		return false;
	}

	public boolean checksRelational(String operator) {

		if (operator.equals("="))
			return true;
		if (operator.equals("!=")) 
			return true;
		if (operator.equals(">=")) 
			return true;
		if (operator.equals("<=")) 
			return true;
		if (operator.equals(">"))
			return true;
		if (operator.equals("<"))
			return true;

		return false;
	}
	
	public static boolean validateRelation(XDFConditional c, String value){		
				
		if(c.getEquation().equals("="))		
			return (c.getValExpected().equals(value.toString())) ? true : false;
		else if(c.getEquation().equals("!="))
			return (!c.getValExpected().equals(value.toString())) ? true : false;
		else if(c.getEquation().equals("<") || c.getEquation().equals(">") || c.getEquation().equals("<=") || c.getEquation().equals(">=")){
			float valueFloat = Float.parseFloat(value);
			float expectedvalueFloat = Float.parseFloat(c.getValExpected());
			
			if(c.getEquation().equals("<"))
				return (valueFloat < expectedvalueFloat) ? true : false;
			else if (c.getEquation().equals("<="))
				return (valueFloat <= expectedvalueFloat) ? true : false;
			else if (c.getEquation().equals(">"))
				return (valueFloat > expectedvalueFloat) ? true : false;
			else if (c.getEquation().equals(">="))
				return (valueFloat >= expectedvalueFloat) ? true : false;			
		}					
		
		return false;
	}
	
	/**
	 * This function will be validate the operation and/or with the results of the conditions of relations
	 * @param cond
	 * @param operations
	 * @return boolean true or false
	 */
	public static boolean  validateOperation(ArrayList<XDFConditional> cond, ArrayList<String> operations){			
				
		boolean result = true;
		boolean first = true;
		
		if(cond.size() == 1){//case we dont have operator AND/OR
			result = cond.get(0).getResult();
			return result;
		}
		
		int j=0;
		for(int i=0; i<cond.size(); i++){
			//for(int j=0; j < operations.size(); j++){//for and / or	
				first = cond.get(i).getResult();
				
				System.out.println("j: " + j + " - operation: " + operations.get(j).toString() + " - i: " + i);
				
				//System.out.println("FIRST: " + cond.get(i).toString());
				
				if(i==0){
					result = cond.get(i+1).getResult();
					//System.out.println("result: " + cond.get(i+1).toString());
					i = 1;//como ja pegou os 2 primeiros itens, o proximo sera a segunda posicao comecando do 0
				}
				
				if(!operations.get(j).equalsIgnoreCase("and")){					
					//System.err.println("OR: result:" + result + " - first: " + first );
					result = result || first;
					//System.err.println("OR result end: " + result );					
				} else {
					//System.err.println("AND: result:" + result + " - first: " + first );					
					result = result && first;
					//System.err.println("AND result end: " + result );					
				}			
			//}
				j++;
		}

		return result;		
	}	
}
